home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / uw2 / bin / gzexe < prev    next >
Text File  |  1997-08-26  |  4KB  |  151 lines

  1. #!/bin/sh
  2. # gzexe: compressor for Unix executables.
  3. # Use this only for binaries that you do not use frequently.
  4. #
  5. # The compressed version is a shell script which decompresses itself after
  6. # skipping $skip lines of shell commands.  We try invoking the compressed
  7. # executable with the original name (for programs looking at their name).
  8. # We also try to retain the original file permissions on the compressed file.
  9. # For safety reasons, gzexe will not create setuid or setgid shell scripts.
  10.  
  11. # WARNING: the first line of this file must be either : or #!/bin/sh
  12. # The : is required for some old versions of csh.
  13. # On Ultrix, /bin/sh is too buggy, change the first line to: #!/bin/sh5
  14.  
  15. x=`basename $0`
  16. if test $# = 0; then
  17.   echo compress executables. original file foo is renamed to foo~
  18.   echo usage: ${x} [-d] files...
  19.   echo   "   -d  decompress the executables"
  20.   exit 1
  21. fi
  22.  
  23. tmp=gz$$
  24. trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
  25.  
  26. decomp=0
  27. res=0
  28. test "$x" = "ungzexe" && decomp=1
  29. if test "x$1" = "x-d"; then
  30.   decomp=1
  31.   shift
  32. fi
  33.  
  34. echo hi > zfoo1$$
  35. echo hi > zfoo2$$
  36. if test -z "`(${CPMOD-cpmod} zfoo1$$ zfoo2$$) 2>&1`"; then
  37.   cpmod=${CPMOD-cpmod}
  38. fi
  39. rm -f zfoo[12]$$
  40.  
  41. tail=""
  42. IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
  43. for dir in $PATH; do
  44.   test -z "$dir" && dir=.
  45.   if test -f $dir/tail; then
  46.     tail="$dir/tail"
  47.     break
  48.   fi
  49. done
  50. IFS="$saveifs"
  51. if test -z "$tail"; then
  52.   echo cannot find tail
  53.   exit 1
  54. fi
  55.  
  56. for i do
  57.   if test ! -f "$i" ; then
  58.     echo ${x}: $i not a file
  59.     res=1
  60.     continue
  61.   fi
  62.   if test $decomp -eq 0; then
  63.     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
  64.       echo "${x}: $i is already gzexe'd"
  65.       continue
  66.     fi
  67.   fi
  68.   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
  69.     echo "${x}: $i has setuid permission, unchanged"
  70.     continue
  71.   fi
  72.   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
  73.     echo "${x}: $i has setgid permission, unchanged"
  74.     continue
  75.   fi
  76.   case "`basename $i`" in
  77.   gzip | tail | chmod | ln | sleep | rm)
  78.     echo "${x}: $i would depend on itself"; continue ;;
  79.   esac
  80.   if test -z "$cpmod"; then
  81.     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
  82.     if test -w $tmp 2>/dev/null; then
  83.       writable=1
  84.     else
  85.       writable=0
  86.       chmod u+w $tmp 2>/dev/null
  87.     fi
  88.   fi
  89.   if test $decomp -eq 0; then
  90.     sed 1q $0 > $tmp
  91.     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
  92. skip=18
  93. if tail +$skip $0 | "/home/hops/bin"/gzip -cd > /tmp/gztmp$$; then
  94.   /bin/chmod 700 /tmp/gztmp$$
  95.   prog="`echo $0 | /bin/sed 's|^.*/||'`"
  96.   if /bin/ln /tmp/gztmp$$ "/tmp/$prog" 2>/dev/null; then
  97.     trap '/bin/rm -f /tmp/gztmp$$ "/tmp/$prog"; exit $res' 0
  98.     (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$ "/tmp/$prog") 2>/dev/null &
  99.     /tmp/"$prog" ${1+"$@"}; res=$?
  100.   else
  101.     trap '/bin/rm -f /tmp/gztmp$$; exit $res' 0
  102.     (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$) 2>/dev/null &
  103.     /tmp/gztmp$$ ${1+"$@"}; res=$?
  104.   fi
  105. else
  106.   echo Cannot decompress $0; exit 1
  107. fi; exit $res
  108. EOF
  109.     "/home/hops/bin"/gzip -cv9 "$i" >> $tmp || {
  110.       /bin/rm -f $tmp
  111.       echo ${x}: compression not possible for $i, file unchanged.
  112.       res=1
  113.       continue
  114.     }
  115.  
  116.   else
  117.     # decompression
  118.     skip=18
  119.     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
  120.       eval `sed -e 1d -e 2q "$i"`
  121.     fi
  122.     if tail +$skip "$i" | "/home/hops/bin"/gzip -cd > $tmp; then
  123.       :
  124.     else
  125.       echo ${x}: $i probably not in gzexe format, file unchanged.
  126.       res=1
  127.       continue
  128.     fi
  129.   fi
  130.   rm -f "$i~"
  131.   mv "$i" "$i~" || {
  132.     echo ${x}: cannot backup $i as $i~
  133.     rm -f $tmp
  134.     res=1
  135.     continue
  136.   }
  137.   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
  138.     echo ${x}: cannot create $i
  139.     rm -f $tmp
  140.     res=1
  141.     continue
  142.   }
  143.   rm -f $tmp
  144.   if test -n "$cpmod"; then
  145.     $cpmod "$i~" "$i" 2>/dev/null
  146.   elif test $writable -eq 0; then
  147.     chmod u-w $i 2>/dev/null
  148.   fi
  149. done
  150. exit $res
  151.